package game;
/*
 * 8.4.12- Daniel Collins.

 * Class to represent the scrabble bag, it randomizes a standard 98 letter scrabble distribution.
 * Knuth shuffle algorithm for randomizing an already populated array taken from the art of computer programming.
 * 
 * constructor method.
 * nextLetter method.
 * 
 * revised on 12.4.12
 */

import java.util.ArrayList;
import java.util.Random;

public class Bag {
	int pointer = 0;
	char[] letters;
	ArrayList<Character> scrabbleBag;
	public Bag(){
		letters = new char[] {
						  'a','a','a','a','a','a','a','a','a',				
						  'b','b',
						  'c','c',
						  'd','d','d','d',
						  'e','e','e','e','e','e','e','e','e','e','e','e',
						  'f','f',
						  'g','g','g',
						  'h','h',
						  'i','i','i','i','i','i','i','i','i',
						  'j',
						  'k',
						  'l','l','l','l',
						  'm','m',
						  'n','n','n','n','n','n',
						  'o','o','o','o','o','o','o','o',
						  'p','p',
						  'q',
						  'r','r','r','r','r','r',
						  's','s','s','s',
						  't','t','t','t','t','t',
						  'u','u','u','u',
						  'v','v',
						  'w','w',
						  'x',
						  'y','y',
						  'z'};
		letters = knuthShuffle(letters);
		scrabbleBag = new ArrayList<Character>(98);
		for (int i =0; i< 98; i++){
			scrabbleBag.add(i, letters[i]);
		}
	}
	//Does a Knuth shuffle on an array of chars its passed
	public char[] knuthShuffle (char[] anArray){
		Random randomGenerator = new Random();
		for(int i = (anArray.length)-1; i > 0; i--){
			int j = randomGenerator.nextInt(i);
			char temp = anArray[j];
			anArray[j]= anArray[i];
			anArray[i]= temp;
			
		}
		return anArray;
	}
	//Returns a letter from the randomized arrayList. and shifts the pointer on to the next element each time it's called.
	public char getLetter(){
		return scrabbleBag.get(pointer++);
	}
	public char[] getLetter(int x){
		char[] temp = new char[x];
		for(int i = 0; i < x; i++){
			temp[i] = this.getLetter();
		}
		return temp;
	}

}